package com.authManager.web.controller;

import com.authManager.commons.feignApi.SysCodeApi;
import com.authManager.commons.feignApi.SysMenuApi;
import com.authManager.commons.feignApi.SysRoleMenuApi;
import com.authManager.entity.SysCode;
import com.authManager.entity.SysMenu;
import com.authManager.entity.SysRole;
import com.authManager.entity.SysRoleMenu;
import com.authManager.web.model.VipAdminMenuData;
import com.commons.base.model.AdminPage;
import com.commons.base.model.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "menu")
public class MenuController extends BaseController {
	public MenuController() {
		System.out.println("初始化-MenuController");
	}

	@Autowired
	SysMenuApi sysMenuApi;
	@Autowired
	SysRoleMenuApi sysRoleMenuApi;
	@Autowired
	SysCodeApi	sysCodeApi;
	@RequestMapping("/list")
	// 系统id
	public String list(@RequestParam(required = true) Long systemId) {
		request.setAttribute("systemId", systemId);
		return "menu/list";
	}

	@RequestMapping("/managerMenu")
	public String managerMenu() {
		ResultData<AdminPage<SysMenu>> data = sysMenuApi.listSysMenu(1, 99999, null, null, null, null, null, null, null,
				null, null, 0, null, null, null, null, null, null, null);
		request.setAttribute("systemList", new ArrayList<>());
		if (data.getIsOk()) {
			request.setAttribute("systemList", data.getData().getRecords());
		}
		return "menu/managerMenu";
	}

	@RequestMapping("/giveList")
	public String giveList(@RequestParam(required = true) Long systemId, @RequestParam(required = true) Long roleId) {
		request.setAttribute("systemId", systemId);
		request.setAttribute("roleId", roleId);
		request.setAttribute("sysMenu", new SysMenu());
		ResultData<SysMenu> data = sysMenuApi.getSysMenu(systemId);
		if (data.getIsOk()) {
			request.setAttribute("sysMenu", data.getData());
		}
		return "menu/giveList";
	}

	@RequestMapping("/giveManagerMenu")
	public String giveManagerMenu(@RequestParam(required = true) Long roleId) {
		request.setAttribute("roleId", roleId);
		ResultData<AdminPage<SysMenu>> data = sysMenuApi.listSysMenu(0, 99999, null, null, null, null, null, null, null,
				null, null, 0, null, null, null, null, null, null, null);
		request.setAttribute("systemList", new ArrayList<>());
		if (data.getIsOk()) {
			request.setAttribute("systemList", data.getData().getRecords());
		}
		return "menu/giveManagerMenu";
	}

	@RequestMapping("/saveGiveData")
	@ResponseBody
	public ResultData<Object> saveGiveData(@RequestParam(required = true) Long sysRoleId,
			@RequestParam(required = false) String sysMenuIds, @RequestParam(required = true) String systemCode) {
		return sysRoleMenuApi.saveSysRoleMenu(sysRoleId, sysMenuIds, systemCode);
	}

	@RequestMapping("/listData")
	@ResponseBody
	public ResultData<AdminPage<SysMenu>> listData(@RequestParam(defaultValue = "1", required = false) Integer pageNum,
			@RequestParam(defaultValue = "100000", required = false) Integer pageSize,
			@RequestParam(required = false) Long sysMenuId, @RequestParam(required = false) String name,
			@RequestParam(required = false) String systemCode, @RequestParam(required = false) String pageUrl,
			@RequestParam(required = false) String pageUrlType, @RequestParam(required = false) Long parentId,
			@RequestParam(required = false) String site, @RequestParam(required = false) Integer sortNum,
			@RequestParam(required = false) String icon, @RequestParam(required = false) Integer menuType,
			@RequestParam(required = false) String description, @RequestParam(required = false) String buttonMark,
			@RequestParam(required = false) String field1, @RequestParam(required = false) String field2,
			@RequestParam(required = false) String field3, @RequestParam(required = false) String start_createTime,
			@RequestParam(required = false) String end_createTime) {
		ResultData<AdminPage<SysMenu>> data = sysMenuApi.listSysMenu(pageNum, pageSize, sysMenuId, name, systemCode,
				pageUrl, pageUrlType, parentId, site, sortNum, icon, menuType, description, buttonMark, field1, field2,
				field3, start_createTime, end_createTime);
		return data;
	}

	@RequestMapping("/vipAdminMenuData")
	@ResponseBody
	public Object vipAdminMenuData(@RequestParam(required = true) String systemCode,
			@RequestParam(required = false) String sysUserId,@RequestParam(required = false) String menuSite) {
		ResultData<SysMenu> sysMenu = sysMenuApi.getSysMenuBySystemCode(systemCode);
		if (sysMenu.getIsOk()) {
			ResultData<List<SysMenu>> sysMenuList = sysMenuApi.listSysMenuByRoleAndUserId("1", systemCode,menuSite);
			if (sysMenuList.getIsOk()) {
				return new VipAdminMenuData(sysMenuList.getData(), sysMenu.getData().getSysMenuId());
			}
		}
		return new VipAdminMenuData(null, null);
	}

	@RequestMapping("/roleMenulistData")
	@ResponseBody
	public ResultData<AdminPage<SysRoleMenu>> roleMenulistData(
			@RequestParam(defaultValue = "1", required = false) Integer pageNum,
			@RequestParam(defaultValue = "10000000", required = false) Integer pageSize,
			@RequestParam(required = false) Long sysRoleMenuId, @RequestParam(required = false) Long sysRoleId,
			@RequestParam(required = false) Long sysMenuId, @RequestParam(required = false) String systemCode,
			@RequestParam(required = false) String start_createTime,
			@RequestParam(required = false) String end_createTime) {
		ResultData<AdminPage<SysRoleMenu>> data = sysRoleMenuApi.listSysRoleMenu(pageNum, pageSize, sysRoleMenuId,
				sysRoleId, sysMenuId, systemCode, start_createTime, end_createTime);
		return data;
	}

	@RequestMapping("/addOrEdit")
	public String addOrEdit(Integer dataId, Integer systemId,Integer menuType) {
		request.setAttribute("is_system", false);
		if (systemId == null) {
			request.setAttribute("is_system", true);
			request.setAttribute("systemName", "系统");
		}
		request.setAttribute("systemId", systemId);
		request.setAttribute("data", new SysRole());
		
		ResultData<AdminPage<SysCode>> menuSiteList = sysCodeApi.listSysCode(1, 9999, null, "menu_site", null, null, null);
		ResultData<AdminPage<SysCode>> buttonMarkList = sysCodeApi.listSysCode(1, 9999, null, "button_mark", null, null, null);
		ResultData<AdminPage<SysCode>> pageSkipList = sysCodeApi.listSysCode(1, 9999, null, "page_skip", null, null, null);
		
		request.setAttribute("menuSiteList", menuSiteList.getData().getRecords());
		request.setAttribute("buttonMarkList", buttonMarkList.getData().getRecords());
		request.setAttribute("pageSkipList", pageSkipList.getData().getRecords());
		
		if (dataId != null) {
			request.setAttribute("parentName", "");
			ResultData<SysMenu> sysRoleData = sysMenuApi.getSysMenu(dataId.longValue());
			if (sysRoleData.getIsOk() && sysRoleData.getData().getParentId() != null) {
				ResultData<SysMenu> sysRoleDataTmp = sysMenuApi.getSysMenu(sysRoleData.getData().getParentId());
				if (sysRoleDataTmp.getIsOk()) {
					request.setAttribute("parentName", sysRoleDataTmp.getData().getName());
				}
			}
			if (sysRoleData.getData().getMenuType() == 0) {
				request.setAttribute("is_system", true);
				request.setAttribute("systemName", "系统");
			}
			request.setAttribute("data", sysRoleData.getData());
			if(sysRoleData.getData().getMenuType()==2) {
				request.setAttribute("systemName", "按钮");
				return "menu/addOrEditByButton";
			}
			return "menu/addOrEdit";
		} else {
			if(menuType!=null&&menuType==2) {
				request.setAttribute("systemName", "按钮");
				return "menu/addOrEditByButton";
			}
			return "menu/addOrEdit";
		}
	}

	@RequestMapping("/addOrEditData")
	@ResponseBody
	public ResultData<Object> addOrEditData(@RequestParam(required = false) Long sysMenuId,
			@RequestParam(required = false) Long systemId, @RequestParam(required = false) String name,
			@RequestParam(required = false) String systemCode, @RequestParam(required = false) String pageUrl,
			@RequestParam(required = false) String pageUrlType, @RequestParam(required = false) Long parentId,
			@RequestParam(required = false) String site, @RequestParam(required = false) Integer sortNum,
			@RequestParam(required = false) String icon, @RequestParam(required = false) Integer menuType,
			@RequestParam(required = false) String description, @RequestParam(required = false) String buttonMark,
			@RequestParam(required = false) String field1, @RequestParam(required = false) String field2,
			@RequestParam(required = false) String field3) {
		if (systemId != null && parentId == null) {
			parentId = systemId;
		}
		if (menuType != null && menuType == 0) {
			parentId = null;
		}
		if (sysMenuId == null) {
			return sysMenuApi.saveSysMenu(name, systemCode, pageUrl, pageUrlType, parentId, site, sortNum, icon,
					menuType, description, buttonMark, field1, field2, field3);
		} else {
			return sysMenuApi.editSysMenu(sysMenuId, name, systemCode, pageUrl, pageUrlType, parentId, site, sortNum,
					icon, menuType, description, buttonMark, field1, field2, field3);
		}
	}

	@RequestMapping("/delData")
	@ResponseBody
	public ResultData<Object> delData(@RequestParam(required = false) Long dataId) {
		return sysMenuApi.delSysMenu(dataId);
	}
}
